package day_2022_12_to_3.year2023;

/**
 * @author haomin
 * @date 2023/04/12 21:37
 **/
public class Day4_12_Num1142 {
    class Solution {
        public int longestDecomposition(String text) {
            int res = 0, n = text.length();
            int l = 0, r = n - 1;
            while (l < r) {
                int end = (l + r) / 2, tR = r;
                boolean f = true;
                while (tR > end) {
                    f = true;
                    for (int i = tR; i <= r; i++) {
                        if (text.charAt(i) != text.charAt(l + i - tR)) {
                            f = false;
                            break;
                        }
                    }
                    if (f) {
                        int tmp = r - tR + 1;
                        r = tR - 1;
                        l += tmp;
                        res += 2;
                        break;
                    }
                    tR--;
                }
                if (!f) {
                    res++;
                    break;
                }
            }
            return res + (l == r ? 1 : 0);
        }
    }
}